package com.heny.demo.util;

import com.heny.demo.entity.TurnPoint;
import lombok.Value;
import org.locationtech.jts.geom.GeometryFactory;
import org.geotools.geometry.jts.*;
import org.geotools.referencing.CRS;
import org.locationtech.jts.geom.Point;
import org.opengis.referencing.FactoryException;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.opengis.referencing.operation.MathTransform;
import org.opengis.referencing.operation.TransformException;

import java.util.List;

/**
 * 坐标系转换工具类
 */

public class CoordinateTransformUtil {
    private static final String SOURCE_CRS = "EPSG:4490";  //CGCS2000
    private static final String TARGET_CRS = "EPSG:4326";  //WGS84

    public static void transformPoints(List<TurnPoint> points) {
        if (points == null || points.isEmpty()) {
            return;
        }
        try {
            CoordinateReferenceSystem sourceCRS = CRS.decode(SOURCE_CRS);
            CoordinateReferenceSystem targetCRS = CRS.decode(TARGET_CRS);
            MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true);

            for (TurnPoint point : points) {
                Point sourcePoint = point.getCoordinate();
                if (sourcePoint != null) {
                    Point transformedPoint = (Point) JTS.transform(sourcePoint, transform);
                    point.setCoordinate(transformedPoint);
                }
            }
        } catch (FactoryException | TransformException e) {
            throw new RuntimeException("坐标转换失败", e);
        }
    }
}